package com.hardroid.workload.ui.fragment;

import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.hardroid.workload.R;
import com.hardroid.workload.ui.EditNoteActivity;
import com.hardroid.workload.ui.EditProjectActivity;
import com.hardroid.workload.utils.Singleton;

public class ProjectOverviewFragment extends Fragment {

	private TextView projectNameView;
	private TextView projectNoteView;
	private TextView observerView;
	private TextView companyView;
	private TextView tallyView;
	private TextView gapView;

	
	
	public final static int EDIT_PROJECT_MENU = 0;
	private static final int EDIT_PROJECT_NOTE_MENU = 1;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		return inflater.inflate(R.layout.fragment_project_overview, container,
				false);
	}

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onActivityCreated(savedInstanceState);

		projectNameView = (TextView) getView().findViewById(R.id.overview_project_name);
		projectNoteView = (TextView) getView().findViewById(R.id.overview_project_note);
		observerView = (TextView) getView().findViewById(R.id.overview_observer);
		companyView = (TextView) getView().findViewById(R.id.overview_company);
		tallyView = (TextView) getView().findViewById(R.id.overview_tally);
		gapView = (TextView) getView().findViewById(R.id.overview_gap);
		
		
		try {

			if (Singleton.currentProject != null) {

				projectNameView.setText(Singleton.currentProject.projectName);
				projectNoteView.setText(Singleton.currentProject.projectNotes);
				observerView.setText(Singleton.currentProject.observerName);
				companyView.setText(Singleton.currentProject.companyName
						+ " , " + Singleton.currentProject.companyLocation);
				tallyView
						.setText(Singleton.currentProject.obsvDesc.nTally + "");
				gapView.setText(Singleton.currentProject.obsvDesc.obsvGap + "");
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		setHasOptionsMenu(true);
	}
	
	
	
	@Override
	public void onResume() {
		// TODO Auto-generated method stub
		super.onResume();
		if (Singleton.currentProject != null) {

			projectNameView.setText(Singleton.currentProject.projectName);
			projectNoteView.setText(Singleton.currentProject.projectNotes);
			observerView.setText(Singleton.currentProject.observerName);
			companyView.setText(Singleton.currentProject.companyName
					+ " , " + Singleton.currentProject.companyLocation);
			tallyView
					.setText(Singleton.currentProject.obsvDesc.nTally + "");
			gapView.setText(Singleton.currentProject.obsvDesc.obsvGap + "");
		}
	}
	
	@Override
	public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
		// TODO Auto-generated method stub
		super.onCreateOptionsMenu(menu, inflater);
		menu.add(0,EDIT_PROJECT_MENU,0,"Ubah keterangan project");
		menu.add(0,EDIT_PROJECT_NOTE_MENU,0,"Ubah catatan singkat");
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// TODO Auto-generated method stub
		if (item.getItemId()==EDIT_PROJECT_MENU){
			startActivity(new Intent(getActivity(), EditProjectActivity.class));
		}else if (item.getItemId()==EDIT_PROJECT_NOTE_MENU){
			startActivity(new Intent(getActivity(),
					EditNoteActivity.class));
		}
		return super.onOptionsItemSelected(item);
	}
	
//	@Override
//	public void onPause() {
//		// TODO Auto-generated method stub
//		super.onPause();
//		Singleton.saveProject(Singleton.currentProject);
//	}
}
